<?php
namespace app\admin\controller;
use think\Request;

/**
 * 新闻管理
 */
class News extends Base
{
	public $req;
	protected $NewsCategory;
	protected $NewsModel;

	function __construct()
	{
		parent::__construct();
		$this->req = Request::instance();
		$this->NewsCategory = model('NewsCategory');
		$this->NewsModel = model('NewsModel');
	}

	/**
	 * 新闻列表
	 */
	public function newsList()
	{
		if ($this->req->isPost()) {
			$data = $this->req->post();
			// halt($data);
			$nlist = $this->NewsModel->getAllByLeftjoin(
				($data['page']-1)*$data['limit'],
				$data['limit']
			);
			$count = $this->NewsModel->count();
			return layuiJson(0, '', $count, $nlist);

		} else {
			return $this->fetch();
		}
	}

	/**
	 * 表格实时编辑
	 */
	public function editFieldById() {
		$data = input('post.');
		$res = $this->NewsModel->save(
			[
				$data['field'] => $data['val'],
				'update_at' => date('Y-m-d H:i:s'),
			],
			[
				'id'=>$data['id']
			]
		);
		if (!$res) {
			return show(1, '更新失败');
		}
		return show(0, '更新成功');
	}

	/**
	 * 添加新闻
	 */
	public function addNews()
	{
		if ($this->req->isPost()) {

			$data = input('post.');
			$news_info['title'] = $data['title'];
			$news_info['desc'] = $data['desc'];
			$news_info['auth'] = $data['auth'];
			$news_info['main_img'] = $data['file'];
			$news_info['nid'] = $data['nid'];
			$news_info['content'] = $data['content'];

			$res = $this->NewsModel->create($news_info);

			// 返回结果
			if ($res) {
				return show(1, '添加成功');
			} else {
				return show(0, '添加失败');
			}

		} else {
			// 获取新闻类型列表
			$nclist = $this->NewsCategory->select();
			return $this->fetch('news/addnews', ['nclist'=>$nclist]);

		}
	}

	/**
	 * 修改新闻
	 */
	public function editnews()
	{
		if ($this->req->isPost()) {
			$data = $this->req->post();
			$data['update_at'] = date('Y-m-d H:i:s');
			$data['main_img'] = $data['file'];
			unset($data['file']);

			// 查询新闻详情
			$info = $this->NewsModel->get($data['id']);
			$file = '.'.$info['main_img'];

			$msg = '';
			// 比对有没有修改主图
			if ($data['main_img'] != $info['main_img']) { 
				// 删除原来的主图
				if (file_exists($file)) {
					if (!unlink($file)) {
						$msg = "删除原主图失败";
					}
				}
			}

			// 更新数据库数据
			$res = $this->NewsModel->save($data,[ 'id'=>$data['id'] ]);
			if (!$res) {
				return show(1, '更新失败'.$msg);
			}
			return show(0, '更新成功'.$msg);

		} else {
			// 获取新闻详情
			$id = input('get.id');
			$newsInfo = $this->NewsModel->get($id);

			// 获取新闻类型列表
			$nclist = $this->NewsCategory->select();

			return $this->fetch('news/editnews', ['newsInfo'=>$newsInfo, 'nclist'=>$nclist]);
		}
	}

	/**
	 * 发布/关闭
	 */
	public function editStatus()
	{
		$data = $this->req->post();

		if ($data['method'] == 'show') {
			$upData = [
				'status' 	 => $data['status'],
				'publish_at' => date('Y-m-d H:i:s')
			];
			$res = $this->NewsModel->save($upData,[ 'id'=>$data['id'] ]);

		} else if ($data['method'] == 'close') {
			$upData = [
				'status' 	=> $data['status'],
				'delete_at' => date('Y-m-d H:i:s')
			];
			// halt($upData);
			$res = $this->NewsModel->save($upData,[ 'id'=>$data['id'] ]);
		}

		if ($res == 1) {
			if ($data['method'] == 'show') {
				return show(0, '发布成功');
			} else if ($data['method'] == 'close') {
				return show(0, '关闭成功');
			}

		} else {
			return show(1, '状态更新失败');
		}
	}

	/**
	 * 预览
	 */
	public function newsdet($id)
	{
		$news_info = $this->NewsModel->get($id);
		return view('news/xiangqing',['news_info'=>$news_info]);
	}
}
